package com.thfw.test.view.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.thfw.wmd.R;
import com.thfw.test.model.authManage.AuthBaseBean;
import com.thfw.test.model.authManage.LampInterfaceBean;
import com.thfw.test.model.bean.LampListBean;
import com.thfw.test.utils.ConfigurationEntry;
import com.thfw.test.utils.StringUtils;
import com.thfw.test.view.activity.LoginActivity;
import com.thfw.test.view.network.AuthManageService;
import com.thfw.test.view.widget.TouchProgessView;

import java.net.MalformedURLException;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import scooper.cn.sc_base.log.SCLog;
import utils.AppActivities;
import utils.RetrofitWrapper;

import static com.thfw.test.utils.FileUtils.TAG;

/**
 * Created BY Cuishikun
 * Created ON 2021/11/12
 * Package Name com.thfw.test.view.fragment
 **/
public class AboutMeFragment extends BaseFragment {

    @BindView(R.id.tv_logout)
    TextView tv_logout;
    @BindView(R.id.tv_name)
    TextView tv_name;

    int port;
    String host;

    public static AboutMeFragment newInstance() {
        AboutMeFragment fragment = new AboutMeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected int getLayoutResource() {
        return R.layout.fm_aboutme;
    }

    @Override
    protected void initView() {

        String name = getConfig().getString(ConfigurationEntry.NAME, "管理员");
        tv_name.setText(name);
        tv_logout.setOnClickListener(v -> {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("确定退出登录");
            builder.setPositiveButton("确定", (dialog, which) -> {
                logout();
            });
            //    设置一个NegativeButton
            builder.setNegativeButton("取消", (dialog, which) -> builder.create().hide());
            //    显示出该对话框
            builder.show();
        });

    }

    @Override
    protected void firstCreate() {

    }


    //区域选择提示

    private void logout() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(getActivity(), url).create(AuthManageService.class).logout(token).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    if (response.code() == 200){
                        AppActivities.instance().finishTo(getActivity(), LoginActivity.class);
                        Toast.makeText(getActivity(),"注销成功",Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

}
